home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 6_14.lha / 6_14 / tstc.c < prev    next >
Text File  |  1993-08-08  |  1KB  |  99 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. include <stream.h>
  6. include <error.h>
  7. include <string.h>
  8. include "6_14c.c"
  9.  
  10. tring::string()
  11.  
  12.    p = new srep;
  13.    p->s = 0;
  14.    p->n = 1;
  15.  
  16.  
  17. tring::string(char* s)
  18.  
  19.    p = new srep;
  20.    p->s = new char[ strlen(s) + 1 ];
  21.    strcpy(p->s, s);
  22.    p->n = 1;
  23.  
  24.  
  25. tring::string(string &x)
  26.  
  27.    x.p->n++;
  28.    p = x.p;
  29.  
  30.  
  31. tring::~string()
  32.  
  33.    if (--p->n == 0) {
  34. delete p->s;
  35. delete p;
  36.    }
  37.  
  38.  
  39. tring &string::operator=(char *s)
  40.  
  41.    if (p->n > 1) {
  42. p->n--;
  43. p = new srep;
  44.    } else if (p->n == 1)
  45. delete p->s;
  46.  
  47.    p->s = new char[strlen(s) + 1];
  48.    strcpy(p->s, s);
  49.    p->n = 1;
  50.    return *this;
  51.  
  52.  
  53. tring &string::operator=(string &x)
  54.  
  55.    x.p->n++;
  56.    if (--p->n == 0) {
  57. delete p->s;
  58. delete p;
  59.    }
  60.    p = x.p;
  61.    return *this;
  62.  
  63.  
  64. stream &operator<<(ostream &out, string &x)
  65.  
  66.    return out << x.p->s << " [" << x.p->n << "]\n";
  67.  
  68.  
  69. stream &operator>>(istream &s, string &x)
  70.  
  71.    char buf[256];
  72.    s >> buf;
  73.    x = buf;
  74.    cout << "echo: " << x << "\n";
  75.    return s;
  76.  
  77.  
  78. ain()
  79.  
  80.    string s = "abc";
  81.    cout << "s = " << s << "\n";
  82.    int i = s == "def";
  83.    cout << "i = " << i << "\n";
  84.  
  85.    string x[100];
  86.    int n;
  87.    cout << "here we go\n";
  88.    for (n = 0; cin >> x[n]; n++) {
  89. string y;
  90. if (n == 100) error("too many strings");
  91. cout << (y = x[n]);
  92. if (y=="done") break;
  93.    }
  94.    cout << "here we go back again\n";
  95.    for (i = n-1; 0<=i; i--)
  96. cout << x[i];
  97.    return 0;
  98.  
  99.